package viewmodel;

import java.util.HashMap;

import componentes.CommunicationManager;

import items.BetItem;
import items.BetSideItem;
import android.app.betmein.R;
import android.app.betmein.activity.DisplayBetActivity;
import android.app.betmein.activity.EntityActivity;
import android.app.betmein.view.BetManagementParticipateTabView;
import android.app.betmein.view.ViewCreatorHelper;
import android.content.Context;
import android.framework.binding.BindableList;
import android.framework.binding.Binding;
import android.framework.binding.ItemList;
import android.framework.binding.MethodRepresent;
import android.framework.paging.BaseViewModel;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

public class BetManagementParticipateTabViewModel extends BaseViewModel<BetManagementParticipateTabView>{

	private ItemList<BetItem> bets;
	
	public BetManagementParticipateTabViewModel(Context context) {
		super(context);
		try {
			bets = new ItemList<BetItem>(context, R.layout.single_participate_bet, ViewCreatorHelper.participateBetListViewCreator(), new HashMap<Integer, MethodRepresent>(){
				{
					put(R.id.chooseSideButton, new MethodRepresent(BetManagementParticipateTabViewModel.class.getDeclaredMethod("onItemClicked", BetItem.class, BetSideItem.class), BetManagementParticipateTabViewModel.this));
				}
			});
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	public void onItemClicked(BetItem item, BetSideItem betSideItem){
		CommunicationManager.getInstance().userChooseNewSide(item, betSideItem);
	}
	
	@Override
	public void onInit() {
		getView().bets.setAdapter(bets);
		
		BindableList<BetItem> participateBets = CommunicationManager.getInstance().getParticipateBets();
		Binding.attached(participateBets, bets);
		getView().bets.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
		getView().bets.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> adapter, View arg1, int pos,
					long arg3) {
				BetItem betItem = (BetItem)adapter.getItemAtPosition(pos);
				startActivity(DisplayBetActivity.class, betItem.getBet());
			}
		});
	}

}
